//
//  ErrorCheckEffect.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/10.
//

import Foundation
import UIKit

class ErrorCheckEffect: UIView, UITextViewDelegate {
    func textViewDidEndEditing(_ textView: UITextView) {
        self.didSelectCheckWith?(self.checkR.isCheck, self.msgTV.text, self.imgPicker.images)
    }
    
    var checkR = CheckEffect()
    
    var isEnable: Bool = true
    
    var title: String = "" {
        didSet {
            checkR.title = title
        }
    }
    
    var didSelectCheckWith:((_ isCheck: Bool?, _ msg: String?, _ imgs: [UIImage]?) -> ())?

    init(withEnable enable: Bool = true) {
        super.init(frame: .zero)
        self.isEnable = enable
        self.isUserInteractionEnabled = enable
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        msgTV.delegate = self

        checkR.delegate = self
        addSubview(checkR)
        checkR.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
            make.bottom.equalTo(self.snp.bottom)
        }
    }
    
    func addErrorView() {
        checkR.snp.remakeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        if errorV.superview == nil {
            addSubview(errorV)
            errorV.snp.makeConstraints { make in
                make.top.equalTo(checkR.snp.bottom).offset(10)
                make.left.right.bottom.equalToSuperview()
            }
            
            if imgPicker.superview == nil {
                errorV.addSubview(imgPicker)
                imgPicker.snp.makeConstraints { make in
                    make.top.equalToSuperview().offset(10)
                    make.left.equalToSuperview()
                    make.right.equalToSuperview()
                }
            }
            
            if msgTV.superview == nil {
                errorV.addSubview(msgTV)
                msgTV.snp.makeConstraints { make in
                    make.left.equalToSuperview().offset(10)
                    make.right.equalToSuperview().offset(-10)
                    make.top.equalTo(imgPicker.snp.bottom)
                    make.height.equalTo(63)
                    make.bottom.equalToSuperview().offset(-10)
                }
            }
        }
    }
    
    func removeErrorView() {
        if errorV.superview != nil {
            errorV.removeFromSuperview()
        }
        
        msgTV.text = ""
        imgPicker.images = []
        imgPicker.collectionView.reloadData()
        
        checkR.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
            make.bottom.equalTo(self.snp.bottom)
        }
    }
    
    lazy var errorV: UIView = {
        let view = UIView.createBase()
        return view
    }()
    
    lazy var msgTV: BSTextView = {
        let l = BSTextView()
        l.placeholder = "请输入描述..."
        l.backgroundColor = .white
        l.layer.masksToBounds = true
        l.layer.cornerRadius = 5
        l.layer.borderWidth = 0.5
        l.layer.borderColor = UIColor.hex("#DEDEDE").cgColor
        l.layer.backgroundColor = UIColor.white.cgColor
        l.font = UIFont.systemFont(ofSize: 14)
        l.textColor = .hex("#333333")
        l.textContainerInset = UIEdgeInsets(top: 10, left: 8, bottom: 10, right: 10)
        return l
    }()
    
    lazy var imgPicker: BSImagePickerView = {
        let picker = BSImagePickerView()
        picker.didSelectImagesWith = {(imgs) -> () in
            self.didSelectCheckWith?(self.checkR.isCheck, self.msgTV.text, imgs)
        }
        return picker
    }()
}

extension ErrorCheckEffect: CheckEffectDelegate {
    func valueChange(_ isCheck: Bool?) {
        if self.isEnable {
            if isCheck != nil {
                addErrorView()
            } else {
                removeErrorView()
            }
            didSelectCheckWith?(checkR.isCheck, msgTV.text, imgPicker.images)
        }
    }
}
